import greenfoot.*; 
import java.util.*;

/**
 * The TitleScreen - the startup screen
 * 
  * @author Jasper Jans
 * This is the main menu
 */
public class TitleScreen extends World
{

    private TitleScreenItem[] menuItems = new TitleScreenItem[2];
    private List<Label> creditLabels;
    
    private int activeMenuItem = 0;
    private boolean inMenu = true;
    
    GreenfootSound rain = new GreenfootSound("rain.mp3");
    
    /*
     * Enabled menu item navigation on the mainscreen
     */
    public void act()
    {

        
        String key = Greenfoot.getKey();
        
        if(inMenu)
        {
            if(key == "down" )
            {
                menuItems[activeMenuItem].setIsSelected(false);
                
                activeMenuItem = activeMenuItem == 1 ? 0 : ++activeMenuItem;
                
                GreenfootSound buttonSwitch = new GreenfootSound("buttonswitch.mp3");
                    
                buttonSwitch.stop();
                buttonSwitch.play();
                menuItems[activeMenuItem].setIsSelected(true);
            }
            
            if(key == "up")
            {
                menuItems[activeMenuItem].setIsSelected(false);
                
                activeMenuItem = activeMenuItem == 0 ? 1 : --activeMenuItem;
                
                GreenfootSound buttonSwitch = new GreenfootSound("buttonswitch.mp3");
                
                buttonSwitch.stop();
                buttonSwitch.play();
                
                menuItems[activeMenuItem].setIsSelected(true);   
            }
            
            if(key == "enter" && inMenu)
            {
                GreenfootSound buttonPress = new GreenfootSound("buttonpress.mp3");
                buttonPress.play();
                
                switch(activeMenuItem)
                {
                    case 0:
                        rain.pause();
                        Greenfoot.setWorld(new GameMap(true));
                        break;
                    case 1:
                        inMenu = false;
                        showCredits();
                        hideMenuItems();
                        break;
                }
            }
        }
        else
        {
            if(key == "escape")
            {
                
                GreenfootSound buttonPress = new GreenfootSound("buttonpress.mp3");
                buttonPress.play();
                inMenu = true;
                hideCredits();
                showMenuItems();
            }
        }
        TitleScreenRainDrop drop = new TitleScreenRainDrop();
        
        addObject(drop, Greenfoot.getRandomNumber(799), 0);
        
    }
    
    /*
     * TitleScreen constructor
     */
    public TitleScreen()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        
        TitleScreenOptions opts = new TitleScreenOptions();
        addObject(opts, 200, 580);
        
       
        TitleScreenLogo logo = new TitleScreenLogo();
        addObject(logo, 400, 150);
        
        
        TitleScreenItem startGameMenuItem = new TitleScreenItem("startgame", true);
        menuItems[0] = startGameMenuItem;
        
        /*TitleScreenItem highScoresMenuItem = new TitleScreenItem("highscores", false);
        menuItems[1] = highScoresMenuItem;*/
        
        TitleScreenItem creditsMenuItem = new TitleScreenItem("credits", false);
        menuItems[1] = creditsMenuItem;
        
        showMenuItems();
        
        setPaintOrder(TitleScreenLogo.class, TitleScreenItem.class, TitleScreenRainDrop.class);
        

        rain.setVolume(50);
        rain.playLoop();
   
    }
    
    /*
     * Shows the menu items 
     */
    private void showMenuItems()
    {
        int y = 250;
        
        for(int i = 0;i<=1;i++){
            addObject(menuItems[i], 400, y);
            y+=50;
        }
    }
    
    /*
     * Hide the menu items
     */
    private void hideMenuItems()
    {
        for(int i = 0;i<=1;i++)
            removeObject(menuItems[i]);
    }
    
    /* 
     * Shows credits on screen
     */
    private void showCredits()
    {
        creditLabels = new ArrayList<Label>();

        Label label1 = new Label(this, 220, 250);
        label1.setText("DINF1B - 0872662 - André de Zwart");

        Label label2 = new Label(this, 220, 280);
        label2.setText("DINF1A - 0874300 - Jasper Jans");
        
        Label label3 = new Label(this, 220, 310);
        label3.setText("DINF1A - 0845026 - Jonathan van Es");
        
        Label label4 = new Label(this, 220, 340);
        label4.setText("DINF1A - 0878883 - Kevin Bos");
        
        creditLabels.add(label1);
        creditLabels.add(label2);
        creditLabels.add(label3);
        creditLabels.add(label4);  

    }
    
    /*
     * Hides the credits
     */
    private void hideCredits()
    {
        for(int i = 0;i<creditLabels.size();i++)
            removeObject(creditLabels.get(i));
    }
    
}
